<template>
  <a-modal
    title="厂区信息"
    :visible="visible"
    :confirm-loading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
    :afterClose="afterClose"
  >
    <a-form-model ref="form" :model="form" :rules="rules" :labelCol="{span:4}" :wrapperCol="{span:20}">
      <a-form-model-item has-feedback label="名称" prop="name">
        <a-input v-model.trim="form.name" autocomplete="off" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
  
</template>

<script>
import {saveFactory} from "../../api/index";
export default {
  name:"FactoryFormModal",
  props:{
    visible:Boolean,
    formData:Object
  },
  watch:{
    formData(newVal,oldVal){
      if(newVal&&newVal.id){
        this.form=newVal;
      }
    }
  },
  data(){
    return {
      loading:false,
      form:{
        name:""
      },
      rules:{
        name:[{ required: true, message: '请输入名称', trigger: 'blur' },],
      },
      factorys:[],
    }
  },
  created(){
  },
  methods:{
    afterClose(){
      this.$refs.form.resetFields();
      this.loading=false;
      this.$emit("update:formData",null);
      this.form={
        name:""
      };
    },
    handleCancel(){
      this.$emit("update:visible",false);
    },
    handleOk(){
      this.$refs.form.validate(valid =>{
        if(valid){ 
          this.loading=true;
          saveFactory(this.form).then(res =>{
            this.loading=false;
            this.$emit("update:visible",false);
            this.$emit("ok");
            this.$message.success(this.form.id?'编辑成功':'添加成功');
          }).catch(err =>{
            this.loading=false;
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>